package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Class;
import cn.edu.ctbu.lddweb.pojo.Grade;
import cn.edu.ctbu.lddweb.pojo.Logon;
import cn.edu.ctbu.lddweb.service.ClassService;
import cn.edu.ctbu.lddweb.service.GradeService;
import cn.edu.ctbu.lddweb.service.LogonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 班级管理Controller
 */
@Controller
public class ClassController {
    @Autowired
    private ClassService classService;

    @Autowired
    private LogonService logonService;

    @Autowired
    private GradeService gradeService;

    //班级管理列表页面
    @RequestMapping("/class/index")
    public String index(Model model, ModelMap map,Class aclass){
        List<Class> class1 = classService.selectList(aclass);
        model.addAttribute("class",class1);

        //教职工下拉列表
        Logon logon = new Logon();
        List<Logon> list = logonService.selectList(logon);
        model.addAttribute("logon",list);
        return "/class/index";
    }

    //搜索/重置
    @RequestMapping("/selectClass")
    public String selectClass(@RequestParam(value = "className",required = false) String className,@RequestParam(value = "teacherName",required = false) String teacherName,@RequestParam(value = "gradeName",required = false) String gradeName, Model model){
        Class aClass = new Class();
        aClass.setClassName(className);
        aClass.setGradeName(gradeName);
        aClass.setTeacherName(teacherName);
        List<Class> list = classService.selectList(aClass);
        model.addAttribute("class",list);

        //教职工下拉列表
        Logon logon = new Logon();
        List<Logon> list1 = logonService.selectList(logon);
        model.addAttribute("logon",list1);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list2 = gradeService.selectList(grade);
        model.addAttribute("grade",list2);
        return "/class/index";
    }

    //新增页面
    @RequestMapping("/class/insert")
    public String insert(Model model){
        //教职工下拉列表
        Logon logon = new Logon();
        List<Logon> list = logonService.selectList(logon);
        model.addAttribute("logon",list);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list2 = gradeService.selectList(grade);
        model.addAttribute("grade",list2);
        return "/class/insert";
    }

    //新增操作
    @RequestMapping("/insertClass")
    public String insertClass(Class class1){
        //判断选择的年级下是否已经有了该班级
        List<Class> list = classService.selectList(class1);//根据班级名称+年级查询，同时满足这两个条件，说明该年级下已经有这个班级，不可以新增了
        if(list.size()!=0){
            return "error111";
        }
        classService.insertClass(class1);
        return "/class/index";
    }


    //修改
    @RequestMapping("/class/edit")
    public String edit(int id,Model model){
        Class class1 = classService.findID(id);
        model.addAttribute("class",class1);

        //教职工下拉列表
        Logon logon = new Logon();
        List<Logon> list = logonService.selectList(logon);
        model.addAttribute("logon",list);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list2 = gradeService.selectList(grade);
        model.addAttribute("grade",list2);
        return "/class/edit";
    }

    //修改操作
    @RequestMapping("/updateClass")
    public String updateClass(Class class1){
        //判断是否重复
        Boolean boo = classService.checkClassUnique(class1);
        if (boo){
            return "error111";
        }
        classService.updateClass(class1);
        return "/class/index";
    }

    //删除
    @RequestMapping("/deleteClass")
    public String deleteClass(int id){
        classService.deleteClass(String.valueOf(id));
        return "/class/index";
    }
}
